/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.cms.service.CategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.entity.ExVideo;
import com.thinkgem.jeesite.modules.cms.service.ExVideoService;

import java.util.ArrayList;
import java.util.List;

/**
 * 视频Controller
 * @author 夏国强
 * @version 2016-05-26
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/exVideo")
public class ExVideoController extends BaseController {

	@Autowired
	private ExVideoService exVideoService;

    @Autowired
    private CategoryService categoryService;
	
	@ModelAttribute
	public ExVideo get(@RequestParam(required=false) String id) {
		ExVideo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = exVideoService.get(id);
		}
		if (entity == null){
			entity = new ExVideo();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:exVideo:view")
	@RequestMapping(value = {"list", ""})
	public String list(ExVideo exVideo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExVideo> page = exVideoService.findPage(new Page<ExVideo>(request, response), exVideo);
		model.addAttribute("page", page);
		return "modules/cms/exVideoList";
	}

	@RequiresPermissions("cms:exVideo:view")
	@RequestMapping(value = "form")
	public String form(ExVideo exVideo, Model model) {


		model.addAttribute("exVideo", exVideo);
		return "modules/cms/exVideoForm";
	}

	@RequiresPermissions("cms:exVideo:edit")
	@RequestMapping(value = "save")
	public String save(String id,ExVideo exVideo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, exVideo)){
			return form(exVideo, model);
		}
		exVideoService.save(exVideo);
		addMessage(redirectAttributes, "保存视频成功");
        String categoryId = exVideo.getCategory()!=null?exVideo.getCategory().getId():null;
		return "redirect:"+Global.getAdminPath()+"/cms/exVideo/?repage&category.id="+(categoryId!=null?categoryId:"");
	}
	
	@RequiresPermissions("cms:exVideo:edit")
	@RequestMapping(value = "delete")
	public String delete(ExVideo exVideo,  String categoryId, @RequestParam(required=false) Boolean isRe,RedirectAttributes redirectAttributes) {
		exVideoService.delete(exVideo,isRe);
		addMessage(redirectAttributes, "删除视频成功");
		return "redirect:"+Global.getAdminPath()+"/cms/exVideo/?repage&category.id="+(categoryId!=null?categoryId:"");
	}

}